home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6015 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  48 lines

  1. Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.misc,owl.development
  2. Path: peer-news.britain.eu.net!uknet!owl-uk!news
  3. From: Kenn@owl-uk.co.uk (Ken Nicolson)
  4. Subject: Compiler Bug - can call private constructors.
  5. Message-ID: <311a149f.8353477@hector>
  6. Sender: news@owl-uk.co.uk (News system)
  7. Organization: Office Workstations Limited
  8. X-Newsreader: Forte Agent .99d/32.182
  9. Date: Thu, 8 Feb 1996 15:58:38 GMT
  10.  
  11. Hi everyone,
  12.  
  13. Running Visual C++ v4.0, I want declare the default constructor as being
  14. private, to ensure that the developer initialises the class with a string,
  15. say, or when I am implementing a Singleton, to ensure that noone else can
  16. create another instance of the class. However, I find that I can return (or
  17. throw) an object created by the default private constructor, without any
  18. warning from the compiler, from anywhere outside the class!
  19.  
  20. Ken
  21.  
  22. PS: Does anyone have an email address for Microsoft Tech Support? I've
  23. searched their website but cannot find anything. I don't fancy phoning
  24. their tech support line and spending half an hour describing a problem to
  25. someone who'll then forward it to someone else, who'll then forward it
  26. again...
  27.  
  28. >>>> SAMPLE CODE REPRODUCING PROBLEM
  29.  
  30. #include <stdio.h>
  31. #include <iostream.h>
  32.  
  33. class FOOBAR
  34. {
  35. private:
  36.     FOOBAR()
  37.     {
  38.         cout << "I've called a private constructor!\n";
  39.     }
  40. };
  41.  
  42.  
  43. FOOBAR foobar()
  44. {
  45. //  FOOBAR b; SYNTAX ERROR, as expected
  46.     return FOOBAR( );   // This is wrong
  47. }
  48.